package cc.fs.study.pinyin;

/**
 * Created by fostion on 6/6/16.
 */
public class Contacts implements Comparable<Contacts> {
    private String name;
    private String pinyin;
    private char firstChar;

    public Contacts(String name, String pinyin) {
        this.name = name;
        this.pinyin = pinyin;
        if (pinyin != null && !pinyin.isEmpty()) {
            String first = pinyin.substring(0, 1);
            if (first.matches("[A-Za-z]")) {
                firstChar = first.toUpperCase().charAt(0);
            } else {
                firstChar = '#';
            }
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPinyin() {
        return pinyin;
    }

    public char getFirstChar() {
        return firstChar;
    }

    public void setFirstChar(char firstChar) {
        this.firstChar = firstChar;
    }

    /**
     * 若不时数字开头的将放到最顶部
     */
    public void setPinyin(String pinyin) {
        this.pinyin = pinyin;
        String first = pinyin.substring(0, 1);
        if (pinyin != null && !pinyin.isEmpty()) {
            if (first.matches("[A-Za-z]")) {
                firstChar = first.toUpperCase().charAt(0);
            } else {
                firstChar = '#';
            }
        }
    }

    @Override
    public int compareTo(Contacts contacts) {
        if (contacts == null) {
            return 0;
        }
        return this.pinyin.compareTo(contacts.getPinyin());
    }
}
